package com.jfinal.weixin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.weixin.model.User;
import com.jfinal.weixin.model.WeixinInfo;
import com.jfinal.weixin.sdk.api.*;
import com.jfinal.weixin.sdk.jfinal.MsgController;
import com.jfinal.weixin.template.DataItem;
import com.jfinal.weixin.template.TempItem;
import com.jfinal.weixin.template.TempToJson;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Crseated by chengzhichao on 16/5/4.
 */
public class IndexController extends Controller {
    public ApiConfig getApiConfig() {
        ApiConfig ac = new ApiConfig(WeixinInfo.dao.getWeixinInfoById(1));
        return ac;
    }

    public void index() {

    }

    /**
     * 获取字体列表
     */
    public void test() {
        java.awt.GraphicsEnvironment eq = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = eq.getAvailableFontFamilyNames();
        for (int i = 0; i < fontNames.length; i++) {
            System.out.println(fontNames[i]);
        }

    }

    public void dodo() {
        render("/_front/socket.html");
    }

    public void login() {
        render("/_back/login/login.html");
    }

    public void admin() {
        render("/_back/admin/index.html");
    }

    public void saveWeixinInfo() {
        WeixinInfo weixinInfo = new WeixinInfo();
        weixinInfo.set("wxname", "红安人");
        weixinInfo.set("appid", "wxc4882f1134eccf65");
        weixinInfo.set("appsecret", "9309df490250d14872e8af577b208f0a");
        weixinInfo.set("token", "chengzhichao");
        renderText(weixinInfo.save() + "");
    }

    /**
     * 获取所有关注者信息
     */
    public void saveUserInfo() {
        ApiConfigKit.setThreadLocalApiConfig(this.getApiConfig());
        ApiResult apiResult = UserApi.getUserInfo("oyHNpvyBm9wx26RJ4Sbj4zAzekT4");
        User user = User.dao.setUserByApiResult(apiResult);
        renderText(user.save() + "");
    }

    public void updatesubscribe() {
        ApiConfigKit.setThreadLocalApiConfig(this.getApiConfig());
        String apiResult = UserApi.getFollows().getJson();
        JSONObject jobj = JSONObject.parseObject(apiResult);
        apiResult = jobj.get("data").toString();
        jobj = JSONObject.parseObject(apiResult);
        apiResult = jobj.get("openid").toString();
        JSONArray data = JSONArray.parseArray(apiResult);

        for (int i = 0; i < data.size(); i++) {
            String openid = data.get(i).toString();
            int a = Db.update("UPDATE user set subscribe=1 where openid = '"+openid+"'");
            System.out.println(a);
        }
    }

    public void updateUsers() {
        ApiConfigKit.setThreadLocalApiConfig(this.getApiConfig());
        String apiResult = UserApi.getFollows().getJson();
        JSONObject jobj = JSONObject.parseObject(apiResult);
        apiResult = jobj.get("data").toString();
        jobj = JSONObject.parseObject(apiResult);
        apiResult = jobj.get("openid").toString();
        JSONArray data = JSONArray.parseArray(apiResult);
        for (int i = 0; i < data.size(); i++) {
            String openid = data.get(i).toString();
            if (User.dao.isJoined(openid) == 0) {
                ApiResult apiResult1 = UserApi.getUserInfo(openid);
                User user = User.dao.setUserByApiResult(apiResult1);
                user.save();
                System.out.println(">>>>>>>>>>保存一个");
            } else {
                ApiResult apiResult1 = UserApi.getUserInfo(openid);
                User user = User.dao.updateUserByApiResult(apiResult1, openid);
                user.update();
                System.out.println(">>>>>>>>>>更新");
            }


        }
        renderNull();
    }


}
